<?php

namespace app\admin\controller;



use app\model\member\MrnaApply;

use app\model\member\Config as ConfigModel;

class Mrna extends BaseAdmin
{

    //加盟审核
    public function lists()
    {
        if (request()->isAjax()){

            $mrna_model = new MrnaApply();

            $page_index = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $search_text = input('search_text', "");

            $apply_no = input('apply_no', "");

            $apply_state = input('apply_state', "");

            $alias = 'a';

            $join = [
                [ 'member m', 'a.member_id = m.member_id', 'inner' ]
            ];

            $condition = [
//                ['apply_state','=',0]
            ];

            if ($apply_state!=''){
                if ($apply_state==0){
                    $condition[] = ['a.apply_state','=',0];
                } elseif ($apply_state==1){
                    $condition[] = ['a.apply_state','>',0];
                }
            }

            if ($search_text){
                $condition[] = [ 'm.nickname', 'like', '%,' . $search_text . ',%' ];
            }

            if ($apply_no){
                $condition[] = [ 'a.apply_no', 'like', '%,' . $apply_no . ',%' ];
            }

            $order = 'a.create_time desc';

            $field = 'a.*,m.nickname,m.headimg,m.mobile';

            $res = $mrna_model->getMrnaApplyPageList($condition, $page_index, $page_size, $order,$field,$alias,$join);

            return $res;
        } else {
            return $this->fetch('mrna/lists');
        }

    }

    //加盟商管理
    public function manage()
    {
        if (request()->isAjax()){

            $mrna_model = new MrnaApply();

            $page_index = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $search_text = input('search_text', "");

            $apply_no = input('apply_no', "");

            $status = input('status','');

            $alias = 'a';

            $join = [
                [ 'member m', 'a.member_id = m.member_id', 'inner' ]
            ];

            $condition = [
                ['a.apply_state','=',4],
                //['a.member_apply','=',0]
            ];

            if ($status){
                $condition[] = [ 'a.status', '=', $status ];
            }

            if ($search_text){
                $condition[] = [ 'm.nickname', 'like', '%,' . $search_text . ',%' ];
            }

            if ($apply_no){
                $condition[] = [ 'a.apply_no', 'like', '%,' . $apply_no . ',%' ];
            }

            $order = 'a.create_time desc';

            $field = 'a.*,m.nickname,m.headimg,m.mobile';

            $res = $mrna_model->getMrnaApplyPageList($condition, $page_index, $page_size, $order,$field,$alias,$join);

            return $res;
        } else {
            return $this->fetch('mrna/manage');
        }

    }

    //退出审核
    public function quit()
    {
        if (request()->isAjax()){

            $mrna_model = new MrnaApply();

            $page_index = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $search_text = input('search_text', "");

            $apply_no = input('apply_no', "");

            $status = input('status','');

            $quit_status = input('quit_status','');

            $alias = 'a';

            $join = [
                [ 'member m', 'a.member_id = m.member_id', 'inner' ]
            ];

            $condition = [
                ['apply_state','=',4],
                ['member_apply','=',1]
            ];

            if ($quit_status!=''){
                if ($quit_status==0){
                    $condition[] = [ 'a.quit_status', '=', 1 ];
                } else {
                    $condition[] = [ 'a.quit_status', '>=', 2 ];
                }
            }

            if ($status){
                $condition[] = [ 'a.status', '=', $status ];
            }

            if ($search_text){
                $condition[] = [ 'm.nickname', 'like', '%,' . $search_text . ',%' ];
            }

            if ($apply_no){
                $condition[] = [ 'a.apply_no', 'like', '%,' . $apply_no . ',%' ];
            }

            $order = 'a.create_time desc';

            $field = 'a.*,m.nickname,m.headimg,m.mobile';

            $res = $mrna_model->getMrnaApplyPageList($condition, $page_index, $page_size, $order,$field,$alias,$join);

            return $res;
        } else {
            return $this->fetch('mrna/quit');
        }
    }


    //入驻信息
    public function detail()
    {
        $apply_id = input('apply_id',0);

        $mrna_model = new MrnaApply();

        $info = $mrna_model->getMrnaApplyInfo([['apply_id','=',$apply_id]]);

        $this->assign('info', $info['data']);

        return $this->fetch('mrna/detail');
    }


    //审核
    public function applyAudit()
    {
        $apply_id = input('apply_id',0);

        $apply_state = input('apply_state',0);

        $reason = input('reason','');

        $mrna_model = new MrnaApply();

        $res = $mrna_model->audit($apply_id,$apply_state,$reason);

        return $res;
    }

    //合同
    public function confrinSign()
    {
        $apply_id = input('apply_id',0);

        $contract_no = input('contract_no','');

        $level = input('level',0);

        $price = input('price',0);

        $mrna_model = new MrnaApply();

        $res = $mrna_model->confrinSign($apply_id,$contract_no,$level,$price);

        return $res;
    }

    //解除加盟商
    public function removeMrna()
    {
        $apply_id = input('apply_id',0);

        $mrna_model = new MrnaApply();

        $res = $mrna_model->removeMrna($apply_id);

        return $res;
    }

    //用户主动退出审核
    public function applyQuitAudit()
    {
        $apply_id = input('apply_id',0);

        $quit_status = input('quit_status',0);

        $quitreason = input('quitreason','');

        $mrna_model = new MrnaApply();

        $res = $mrna_model->quitAudit($apply_id,$quit_status,$quitreason);

        return $res;
    }

    //提交汇付
    public function submitHuifu()
    {
        $apply_id = input('apply_id',0);

        $mrna_model = new MrnaApply();

        $res = $mrna_model->submitHuifu($apply_id);

        return $res;
    }

    //权益介绍
    public function agreement()
    {
        if (request()->isAjax()){
            //设置注销协议
            $title = input('title', '');
            $content = input('content', '');
            $config_model = new ConfigModel();
            return $config_model->setMemberMrnaDocument($title, $content, 0, 'admin');
        } else {
            //获取注销协议
            $config_model = new ConfigModel();
            $document_info = $config_model->getMemberMrnaDocument(0, 'admin');
            $this->assign('document_info', $document_info);

            return $this->fetch('mrna/agreement');
        }

    }

    //推广说明
    public function recommend()
    {
        if (request()->isAjax()){
            //设置注销协议
            $title = input('title', '推广说明');
            $content = input('content', '');
            $config_model = new ConfigModel();
            return $config_model->setMrnaRecommendDocument($title, $content, 0, 'admin');
        } else {
            //获取注销协议
            $config_model = new ConfigModel();
            $document_info = $config_model->getMrnaRecommendDocument(0, 'admin');
            $this->assign('document_info', $document_info);

            return $this->fetch('mrna/recommend');
        }

    }
}